﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TfsBacklogMgr.Main;

namespace NUnitTests
{
    [TestFixture]
    public class TestIterations
    {
        [TestFixtureSetUp]
        public void Setup()
        {
            Factory.IsTesting = true;
            Factory.GetFactory().LoadSettings("dummy");
        }

        private bool ContainsIterNamed(IEnumerable<Iteration> iters, string iterPath)
        {
            bool res = false;
            foreach (var iter in iters)
            {
                if (iter.Path == iterPath)
                    res = true;
            }
            return res;
        }

        [Test]
        public void TestIterationsAll()
        {
            var iters = Iteration.GetAllIterations();
            Assert.IsTrue( ContainsIterNamed( iters, "$" ), "Root iter");
            Assert.IsTrue( ContainsIterNamed( iters, "$/7.1" ), "7.1 iter");
            Assert.IsTrue( ContainsIterNamed( iters, "$/7.1/Stake" ), "Stake iter");
            Assert.IsTrue( ContainsIterNamed( iters, "$/7.1/Stake/Sprint 1"), "Stake/Sprint1 iter");
            Assert.IsTrue( ContainsIterNamed( iters, "$/7.1/Stake/Sprint 2"), "Stake/Sprint 2 iter");
            Assert.IsFalse(ContainsIterNamed( iters, "$/7.1/Stake/Sprint 3"), "Stake/Sprint 3 iter");
            Assert.IsTrue( ContainsIterNamed( iters, "$/7.1/Stake/Backlog"), "Stake/Backlog iter");
            Assert.IsTrue( ContainsIterNamed( iters, "$/7.1/Find"), "Find iter");
            Assert.IsFalse( ContainsIterNamed( iters, "$/7.1/Find/Backlog"), "Find/Backlog iter");
            Assert.IsTrue( ContainsIterNamed( iters, "$/7.1/Find/Sprint 1"), "Find/Sprint1 iter");
            Assert.IsFalse(ContainsIterNamed(iters, ""), "blank iter");
        }

        [Test]
        public void TestIterationsBacklog()
        {
            var iters = Context.GetContext().BacklogIterations;
            Assert.IsFalse(ContainsIterNamed(iters, "$"), "Root iter");
            Assert.IsTrue(ContainsIterNamed(iters, "$/7.1"), "7.1 iter - parent of backlog iterations");
            Assert.IsTrue(ContainsIterNamed(iters, "$/7.1/Stake"), "Stake iter is backlog");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Stake/Sprint 3"), "Stake/Sprint 3 iter is not backlog");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Stake/Backlog"), "Stake/Backlog iter is explicitly backlog, but is contained by backlog iter");
            Assert.IsTrue(ContainsIterNamed(iters, "$/7.1/Find"), "Find iter is backlog");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Find/Backlog"), "Find/Backlog iter does not exist");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Find/Sprint 1"), "Find/Sprint1 iter is not backlog");
            Assert.IsFalse(ContainsIterNamed(iters, ""), "blank iter");
        }

        [Test]
        public void TestIterationsSprint()
        {
            var iters = Context.GetContext().NonLeafIterations;
            Assert.IsTrue(ContainsIterNamed(iters, "$"), "Root iter");
            Assert.IsTrue(ContainsIterNamed(iters, "$/7.1"), "7.1 iter - parent of backlog iterations");
            Assert.IsTrue(ContainsIterNamed(iters, "$/7.1/Stake"), "Stake iter is backlog, but non-leaf");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Stake/Sprint 1"), "Stake/Sprint 1 iter is leaf");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Stake/Sprint 2"), "Stake/Sprint 2 iter is leaf");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Stake/Sprint 3"), "Stake/Sprint 3 iter is leaf");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Stake/Backlog"), "Stake/Backlog iter is explicitly backlog + leaf");
            Assert.IsTrue(ContainsIterNamed(iters, "$/7.1/Find"), "Find iter is backlog, but non-leaf");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Find/Backlog"), "Find/Backlog iter does not exist");
            Assert.IsFalse(ContainsIterNamed(iters, "$/7.1/Find/Sprint 1"), "Find/Sprint1 iter is leaf");
            Assert.IsFalse(ContainsIterNamed(iters, ""), "blank iter");
        }


        [Test]
        public void TestWorkitemsInIter()
        {
        }
    }
}
